package com.jiaai.cloud.chatroom.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jiaai.cloud.base.exception.ServiceException;
import com.jiaai.cloud.base.page.FrontPage;
import com.jiaai.cloud.base.util.RestResultGenerator;
import com.jiaai.cloud.chatroom.entity.TChatRoomColumn;
import com.jiaai.cloud.chatroom.entity.TChatRoomColumnSearch;
import com.jiaai.cloud.chatroom.service.ITChatRoomColumnService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 聊天室栏目 前端控制器
 * </p>
 *
 * @author 佳爱网络科技
 * @since 2017-08-01
 */
@Controller
@RequestMapping("/api/manage/tChatRoomColumn")
public class TChatRoomColumnController {
    @Autowired
    private ITChatRoomColumnService itChatRoomColumnService;

    //查找所有的
    @RequestMapping(value = "/findByAll", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object findByAll(TChatRoomColumnSearch search) throws ServiceException {
        try {

            EntityWrapper wrapper=new EntityWrapper();
            if(!StringUtils.isEmpty(search.getName())){
                wrapper.like("name",search.getName());
            }
            return RestResultGenerator.genSuccessResult( itChatRoomColumnService.selectPage(search.getPagePlus(),wrapper));
        } catch (Exception e) {
            throw new ServiceException("查询失敗", e.getMessage());
        }
    }

    //    删除
    @RequestMapping(value = "/deleteId/{id}", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public Object deleteId(@PathVariable("id") Integer id) throws ServiceException {
        try {
            itChatRoomColumnService.deleteById(id);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }

    //改(有就改，没有就插入)
    @RequestMapping(value="/edit",method=RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object edit(@RequestBody TChatRoomColumn tChatRoomColumn)throws ServiceException{
        try {
            tChatRoomColumn.setCreateTime(new Date());
            itChatRoomColumnService.insertOrUpdate(tChatRoomColumn);
            return null;
        } catch (Exception e) {
            throw new  ServiceException("编辑失敗",e.getMessage());
        }
    }

	
}
